#include "modifyinfo.h"
#include "ui_modifyinfo.h"
#include "addperson.h"
#include "addtime.h"
#include <QMessageBox>
ModifyInfo::ModifyInfo(QSettings *settings, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ModifyInfo)
{
    ui->setupUi(this);
    mSettings=settings;
    QStringList list;
    QVariant variant=mSettings->value("person");
    list=variant.toStringList();

    for(QString s:list){
        ui->cbPerson->addItem(s);
    }
    connect(ui->cbPerson,SIGNAL(currentIndexChanged(int)),this,SLOT(onCbPersonCurrentIndexChanged(int)));
    connect(ui->dsbWorkTime,SIGNAL(valueChanged(double)),this,SLOT(onDsbWorkTimeValueChanged(double)));
    if(ui->cbPerson->count()>0){
        onCbPersonCurrentIndexChanged(0);
        ui->cbPerson->setCurrentIndex(0);
        ui->cbPerson->setEnabled(true);
        ui->btnPersonDelete->setEnabled(true);
        ui->dsbWorkTime->setEnabled(true);
    }
    else{
        ui->cbPerson->setEnabled(false);
        ui->btnPersonDelete->setEnabled(false);
        ui->dsbWorkTime->setEnabled(false);
    }
    list=mSettings->value("time").toStringList();
    for(QString s:list){
        QStringList tmp=s.split("->");
        QTime start=QTime::fromString(tmp[0]);
        QTime end=QTime::fromString(tmp[1]);
        ui->cbTime->addItem(QString("%1(%2h)").arg(s).arg(end.hour()-start.hour()+(end.minute()-start.minute())/60.0,0,'f',1));
    }
    connect(ui->cbTime,SIGNAL(currentIndexChanged(int)),this,SLOT(onCbTimeCurrentIndexChanged(int)));
    connect(ui->sbPersonOfTime,SIGNAL(valueChanged(int)),this,SLOT(onSbPersonOfTimeValueChanged(int)));
    if(ui->cbTime->count()>0){
        onCbTimeCurrentIndexChanged(0);
        ui->cbTime->setCurrentIndex(0);
        ui->cbTime->setEnabled(true);
        ui->btnTimeDelete->setEnabled(true);
        ui->sbPersonOfTime->setEnabled(true);
    }
    else{
        ui->cbTime->setEnabled(false);
        ui->btnTimeDelete->setEnabled(false);
        ui->sbPersonOfTime->setEnabled(false);
    }
    connect(ui->btnPersonAdd,SIGNAL(clicked()),this,SLOT(onBtnPersonAddClicked()));
    connect(ui->btnPersonDelete,SIGNAL(clicked()),this,SLOT(onBtnPersonDeleteClicked()));
    connect(ui->btnTimeAdd,SIGNAL(clicked()),this,SLOT(onBtnTimeAddClicked()));
    connect(ui->btnTimeDelete,SIGNAL(clicked()),this,SLOT(onBtnTimeDeleteClicked()));
}

ModifyInfo::~ModifyInfo()
{
    delete ui;
}


void ModifyInfo::onCbPersonCurrentIndexChanged(int idx)
{
    bool b;
    double value=mSettings->value("person:"+ui->cbPerson->itemText(idx)).toDouble(&b);
    if(b)
    {
        ui->dsbWorkTime->setValue(value);
    }
}

void ModifyInfo::onDsbWorkTimeValueChanged(double value)
{
    mSettings->setValue("person:"+ui->cbPerson->currentText(),value);
}

void ModifyInfo::onCbTimeCurrentIndexChanged(int idx)
{

    bool b;
    int value=mSettings->value("time:"+ui->cbTime->itemText(idx).split("(")[0]).toInt(&b);
    if(b)
    {
        ui->sbPersonOfTime->setValue(value);
    }
}

void ModifyInfo::onSbPersonOfTimeValueChanged(int value)
{

    mSettings->setValue("time:"+ui->cbTime->currentText().split("(")[0],value);
}

void ModifyInfo::onBtnPersonAddClicked()
{
    AddPerson dialog(this);
    if(dialog.exec()==QDialog::Accepted)
    {
        QString name=dialog.name();
        QString oriname=name;
        QStringList list=mSettings->value("person").toStringList();
        int i=1;
        while(list.contains(name)){
            name=QString("%1%2").arg(oriname).arg(i++);
        }
        ui->cbPerson->addItem(name);
        list.append(name);
        mSettings->setValue("person",list);
        mSettings->setValue("person:"+name,0);
        ui->dsbWorkTime->setValue(0);
        ui->cbPerson->setCurrentIndex(ui->cbPerson->count()-1);
        ui->cbPerson->setEnabled(true);
        ui->btnPersonDelete->setEnabled(true);
        ui->dsbWorkTime->setEnabled(true);
    }
}

void ModifyInfo::onBtnPersonDeleteClicked()
{
    QString name=ui->cbPerson->currentText();
    QStringList list=mSettings->value("person").toStringList();
    list.removeOne(name);
    mSettings->setValue("person",list);
    if(mSettings->contains("person:"+name)){
        mSettings->remove("person:"+name);
    }
    int idx=ui->cbPerson->findText(name);
    if(idx!=-1){
        ui->cbPerson->removeItem(idx);
    }
    if(ui->cbPerson->count()<=0){
        ui->cbPerson->setEnabled(false);
        ui->btnPersonDelete->setEnabled(false);
        ui->dsbWorkTime->setEnabled(false);
    }

}

void ModifyInfo::onBtnTimeAddClicked()
{
    AddTime dialog(mSettings,this);
    if(dialog.exec()==QDialog::Accepted)
    {

        QTime st=dialog.start();
        QTime en = dialog.end();
        QString start=st.toString("HH:mm");
        QString end=en.toString("HH:mm");
        QString time=start+"->"+end;
        QStringList list=mSettings->value("time").toStringList();
        list.append(time);
        ui->cbTime->addItem(QString("%1(%2h)").arg(time).arg(en.hour()-st.hour()+(en.minute()-st.minute())/60.0,0,'f',1));
        mSettings->setValue("time",list);
        mSettings->setValue("time:"+time,0);
        ui->sbPersonOfTime->setValue(0);
        ui->cbTime->setCurrentIndex(ui->cbTime->count()-1);
        ui->cbTime->setEnabled(true);
        ui->btnTimeDelete->setEnabled(true);
        ui->sbPersonOfTime->setEnabled(true);

    }

}

void ModifyInfo::onBtnTimeDeleteClicked()
{
    QString time=ui->cbTime->currentText();
    QString time2=time.split("(")[0];
    QStringList list=mSettings->value("time").toStringList();
    list.removeOne(time2);
    mSettings->setValue("time",list);
    if(mSettings->contains("time:"+time2)){
        mSettings->remove("time:"+time2);
    }
    int idx=ui->cbTime->findText(time);
    if(idx!=-1){
        ui->cbTime->removeItem(idx);
    }
    if(ui->cbTime->count()<=0){
        ui->cbTime->setEnabled(false);
        ui->btnTimeDelete->setEnabled(false);
        ui->sbPersonOfTime->setEnabled(false);
    }

}

